package com.hfun.server.controller${packageName};

import com.alibaba.fastjson.TypeReference;
import com.hfun.common.utils.JsonUtil;
import com.hfun.server.domain${packageName}.${objectName}Model;
import com.hfun.domain.request.HttpDataParam;
import com.hfun.domain.response.ResponseData;
import com.hfun.server.common.IConstants;
import com.hfun.server.service${packageName}.${objectName}Service;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.Field;

/**
 * ${description}
 * Created by AUTO on ${nowDate?string("yyyy/MM/dd")}.
 */
@Controller
@RequestMapping("${objectNameLower}")
public class ${objectName}Controller extends BaseController{

    private static final Logger logger = Logger.getLogger(${objectName}Controller.class);
    @Resource
    private ${objectName}Service ${objectNameLower}Service;

    /**
     * 初始化字段信息
     */
    @Override
    protected void initBaseFields() {
        Class clazz = ${objectName}Model.class;
        Field[] fields = clazz.getDeclaredFields();
        for (Field f:fields) {
            //大写字母转小写并添加下划线
            BASE_FIELDS.put(f.getName(),f.getName().replaceAll("[A-Z]","_$0").toLowerCase());
        }
    }

    /**
     * 查询列表
     * @param data
     * @return
     */
    @RequestMapping("/queryList")
    @ResponseBody
    public String queryList(String data){
        try {
            ${objectName}Model model= JsonUtil.getObject(data, ${objectName}Model.class);
            ResponseData queryResultData = super.checkCRUDParam(
                    IConstants.ParamDefine.CrudEnum.QUERY, model);
            if (queryResultData.getCode() != SUCCESS) {
                return JsonUtil.toJSONString(queryResultData, false);
            }
            return ${objectNameLower}Service.queryList(model);
        } catch (Exception e) {
            logger.error("queryList error--param:"+ data + e.toString(),e);
            return JsonUtil.toJSONString(new ResponseData(IConstants.ResponseDefine.Code.ERROR_500.getIndex(), "程序异常:"+e.toString()), false);
        }
    }

    /**
     * 修改
     * @param data
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(String data){
        try {
            HttpDataParam<${objectName}Model> dataCondition = JsonUtil.parseObject(data, new TypeReference<HttpDataParam<${objectName}Model>>(){});
            return ${objectNameLower}Service.update(dataCondition);
        }catch(DataAccessException e){
            int code = super.getSqlErrorCode(e);
            if(code == IConstants.ResponseDefine.Code.ERROR_506.getIndex()){
                logger.warn("update error--param:"+ data + e.toString());
            }else{
                logger.error("update error--param:"+ data + e.toString(),e);
            }
            return JsonUtil.toJSONString(new ResponseData(code,"SQL异常:"+e.getCause()),false);
        } catch (Exception e) {
            logger.error("update error--param:"+ data + e.toString(),e);
            return JsonUtil.toJSONString(new ResponseData(IConstants.ResponseDefine.Code.ERROR_500.getIndex(), "程序异常:"+e.toString()), false);
        }
    }

    /**
     * 新增
     * @param data
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(String data){
        try {
            HttpDataParam<${objectName}Model> dataCondition = JsonUtil.parseObject(data, new TypeReference<HttpDataParam<${objectName}Model>>(){});
            return ${objectNameLower}Service.insert(dataCondition);
        }catch(DataAccessException e){
            int code = super.getSqlErrorCode(e);
            if(code == IConstants.ResponseDefine.Code.ERROR_506.getIndex()){
                logger.warn("update error--param:"+ data + e.toString());
            }else{
                logger.error("update error--param:"+ data + e.toString(),e);
            }
            return JsonUtil.toJSONString(new ResponseData(code,"SQL异常:"+e.getCause()),false);
        } catch (Exception e) {
            logger.error("update error--param:"+ data + e.toString(),e);
            return JsonUtil.toJSONString(new ResponseData(IConstants.ResponseDefine.Code.ERROR_500.getIndex(), "程序异常:"+e.toString()), false);
        }
    }

    /**
    * 删除
    * @return
    */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String data){
        try {
            HttpDataParam<${objectName}Model> dataCondition = JsonUtil.parseObject(data, new TypeReference<HttpDataParam<${objectName}Model>>(){});
            return ${objectNameLower}Service.delete(dataCondition);
        }catch(DataAccessException e){
            int code = super.getSqlErrorCode(e);
            if(code == IConstants.ResponseDefine.Code.ERROR_506.getIndex()){
                logger.warn("delete error--param:"+ data + e.toString());
            }else{
                logger.error("delete error--param:"+ data + e.toString(),e);
            }
            return JsonUtil.toJSONString(new ResponseData(code,"SQL异常:"+e.getCause()),false);
        } catch (Exception e) {
            logger.error("delete error--param:"+ data + e.toString(),e);
            return JsonUtil.toJSONString(new ResponseData(IConstants.ResponseDefine.Code.ERROR_500.getIndex(), "程序异常:"+e.toString()), false);
        }
    }
}
